package com.calciominoBeta.Activity;

import java.util.ArrayList;

import org.json.JSONException;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

//import com.calciomio.app.R;
import com.calciomio.app.R;
import com.calciominoBeta.Entity.InfoOj;
import com.calciominoBeta.Util.Sdata;
import com.calciominoBeta.Util.Util;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;
import com.telpoo.frame.utils.Mlog;

public class AQDetailActivity extends FragmentActivity {

	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;
	private ImageView img_next, img_prev, img_back, img_share, img_share_post;
	ArrayList<InfoOj> dataLv = null;
	Integer position = 1;
	ListView lv_accueil;
	protected int itemHeight;
	private AdView adView;
	private ViewPager viewPager;
	NgPageViewAdapter adapter;
	ProgressDialog progressDialog;
	int temcurepare = 0;
	int next = 1;
	int prev = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);

		dataLv = Sdata.dataLv;
		position = Sdata.position;
		Sdata.dataLv = null;
		Sdata.position = null;

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			position = 0;
			dataLv = new ArrayList<InfoOj>();
			String gcm_message = bundle.getString("gcm_message");
			Mlog.T("gcm_message=" + gcm_message);
			if (gcm_message != null) {
				try {
					BaseObject ojGcm = JsonSupport
							.jsonObject2BaseOj(gcm_message);
					ArrayList<BaseObject> ojs = new ArrayList<BaseObject>();
					ojs.add(ojGcm);
					ArrayList<InfoOj> ojg = Util.convertOj(ojs);
					dataLv = ojg;

				} catch (JSONException e) {
					e.printStackTrace();
					
					
					
					
					
					
					finish();
				}

			} else
				finish();
		}

		img_next = (ImageView) findViewById(R.id.img_next);
		img_prev = (ImageView) findViewById(R.id.img_prev);
		viewPager = (ViewPager) findViewById(R.id.pager_container_web);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_share = (ImageView) findViewById(R.id.img_share);
		ImageView[] img={img_back,img_share,img_next,img_prev};
		Util.hilightBtn(img);
		
		img_share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				BaseDetailfm fm=(BaseDetailfm) adapter.instantiateItem(viewPager, viewPager.getCurrentItem());
				fm.share("");
			}
		});
		FragmentManager fragmentManager = getSupportFragmentManager();
		adapter = new NgPageViewAdapter(fragmentManager, dataLv,
				getBaseContext());
		viewPager.setAdapter(adapter);

		adView = (AdView) findViewById(R.id.ad);
		AdRequest adRequest = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
				.build();
		adView.loadAd(adRequest);

		img_next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
			}

		});
		img_prev.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
			}
		});
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				int dem = 0;
				int cuoi=0;
				dem = (position - 2 < 0 ? 0 : position - 2);
				cuoi=(position+2>adapter.getCount()-1?adapter.getCount():position+2);
				for (int i = dem; i < cuoi; i++) {
					MyFragmentInterface fg = (MyFragmentInterface) adapter
							.instantiateItem(viewPager, i);
					if (position == i) {
						fg.onVisible(position);
					} else {
						fg.onInVisible(position);
					}
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		
		if(position!=null&&viewPager!=null)
		viewPager.setCurrentItem(position, false);

	}

}
